/* 
 *  FileName: mypoll_test.c - Test for poll() in driver.
 *
 *  Author: He Jingbo   jbhe@isoftstone.com
 *
 *  Date: Mon 10 Jan 2011 04:53:32 PM CST
 */

#include <stdio.h>
#include <fcntl.h>
#include <sys/select.h>

#define FIFO_CLEAR  0x1
#define BUFFER_LEN  20

int main(int argc, char *argv[])
{
    int fd, num;
    char rd_ch[BUFFER_LEN];
    fd_set rfds, wfds;      /* 读,写文件描述符集 */
    
    /* 以非阻塞方式打开/dev/globalfifo */
    fd = open("/dev/globalfifo", O_RDONLY|O_NONBLOCK);
    if(fd != -1)
    {
        /* FIFO 清0 */
        if(ioctl(fd, FIFO_CLEAR, 0) < 0)
        {
            printf("ioctl command failed \n");
        }
        while(1)
        {
            FD_ZERO(&rfds);
            FD_ZERO(&wfds);
            FD_SET(fd, &rfds);
            FD_SET(fd, &wfds);

            select(fd + 1, &rfds, &wfds, NULL, NULL);

            /* 数据可读取 */
            if(FD_ISSET(fd, &rfds))
            {
                printf("Poll monitor: can be read.\n");
            }
            /* 设备可写 */
            if(FD_ISSET(fd, &wfds))
            {
                printf("Poll monitor: can be written.\n");
            }
        }
    }
    else
    {
        printf("Device open failure\n");
    }

    return 0;
}
